﻿using ControlzEx.Theming;
using Microsoft.Win32;
using SmartDevice.Models;
using Stylet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SmartDevice.Pages
{
    public class MainShellViewModel : Screen
    {
        private readonly IWindowManager _windowManager;
        private readonly IViewFactory _viewFactory;
        private readonly IEventAggregator _events;

        public MainShellViewModel(IWindowManager windowManager, IViewFactory viewFactory, IEventAggregator events)
        {
            _windowManager = windowManager;
            _viewFactory = viewFactory;
            _events = events;
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            InitSettings();
        }

        #region 基本绑定

        public int ProgressValue { get; set; }
        public bool IsProgressShow { get; set; } = true;
        public Visibility ProgressVisibility => IsProgressShow ? Visibility.Visible : Visibility.Collapsed;

        #endregion

        #region 命令

        public string InputString { get; set; }
        public string OutputString { get; set; } = "hehe";
        public void ShowString()
        {
            OutputString = $"Your string is : {InputString}";
        }
        public bool CanShowString => !string.IsNullOrEmpty(InputString);

        #endregion

        #region 绑定列表

        public BindableCollection<string> StringList { get; set; } = new BindableCollection<string>();
        public string SelectedString { get; set; }
        private int Count = 1;

        public void AddString()
        {
            StringList.Add($"Item{Count++}");
        }

        public void DeleteString()
        {
            StringList.Remove(SelectedString);
        }

        public bool CanDeleteString => SelectedString != null;

        #endregion

        #region 顶部工具条
        public bool IsSettingFlyoutOpen { get; set; }
        public void DoSetting()
        {
            IsSettingFlyoutOpen = !IsSettingFlyoutOpen;
        }
        public bool CanDoSetting => true;

        public void DoHelp()
        {
        }


        #endregion

        #region 设置

        /// <summary>
        /// 初始化设置
        /// </summary>
        private void InitSettings()
        {
            InitThemeSetting();
        }

        /// <summary>
        /// 打开设置页面
        /// </summary>
        public void SettingPageOpeningFinished()
        {
            //初始化配置          
        }

        /// <summary>
        /// 关闭设置页面
        /// </summary>
        public void SettingPageClosingFinished()
        {
            //保存相关配置
            Properties.Settings.Default.ThemeBackground = BackgroundName;
            Properties.Settings.Default.ThemeForeground = SelectedAccentColor.Name;
            Properties.Settings.Default.Save();
        }


        #region 主题

        private string BackgroundName;

        public bool ThemeBackgroundDarkChecked { get; set; }
        public bool ThemeBackgroundLightChecked { get; set; }

        public List<AccentColorData> AccentColors { get; set; }
        public AccentColorData SelectedAccentColor { get; set; }

        private void InitThemeSetting()
        {
            BackgroundName = Properties.Settings.Default.ThemeBackground;
            ThemeBackgroundDarkChecked = BackgroundName == "Dark";
            ThemeBackgroundLightChecked = !ThemeBackgroundDarkChecked;

            AccentColors = ThemeManager.Current.Themes
                                          .GroupBy(x => x.ColorScheme)
                                          .OrderBy(a => a.Key)
                                          .Select(a => new AccentColorData { Name = a.Key, ColorBrush = a.First().ShowcaseBrush })
                                          .ToList();

            SelectedAccentColor = new AccentColorData { Name = Properties.Settings.Default.ThemeForeground };

            this.Bind(s => SelectedAccentColor, (o, e) => AccentColorChanged());
        }

        public void ThemeBackgroundLightClick()
        {
            BackgroundName = "Light";
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        public void ThemeBackgroundDarkClick()
        {
            BackgroundName = "Dark";
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        public void AccentColorChanged()
        {
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        #endregion



        #endregion
    }
}
